package controller;

import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import model.bean.User;
import model.dao.Database;
import view.LoginView;
import view.PBoxView;
import view.PortraitView;
import view.RegisterView;

import java.io.IOException;
import java.util.Optional;
import java.util.Random;

public class RegisterController {

    public AnchorPane signInPane;
    public PasswordField re_password;
    public PasswordField re_recheck_pwd;
    public CheckBox re_sex_man;
    public CheckBox re_sex_woman;
    public TextField re_phone;
    public Button register_btn;
    public String account;
    public TextField re_nickname;
    public DatePicker re_birthday;
    public Label label1;
    public Label label2;
    public Label label3;
    public Label label7;
    public TextField re_email;
    public Button headPortrait;
    private String nickname;
    private String password;
    private String sex = "2";
    private String birthday;
    private String phone;
    private String email;

    public static String imgPath = "css/img/head/5.png";

    /**
     * 性别选择保证单选方法，sex初值为"2"表示保密，男性sex为"0"，女性为"1"
     * @param actionEvent
     */
    public void isManChoose(ActionEvent actionEvent) {
        if (re_sex_man.isSelected()) {
            sex = "0";
            if (re_sex_woman.isSelected()) {
                re_sex_woman.setSelected(false);
            }
        }
    }
    public void isWomanChoose(ActionEvent actionEvent) {
        if (re_sex_woman.isSelected()) {
            sex = "1";
            if (re_sex_man.isSelected()) {
                re_sex_man.setSelected(false);
            }
        }
    }

    /**
     * 确定注册点击事件
     * 检测数据是否合法，并向数据库提交数据
     * @param actionEvent
     * @throws Exception
     */
    public void signUp(ActionEvent actionEvent) throws Exception {
        if (checkInfo()) {
            account = createAccountNum();
            User user = new User(account,nickname,password,sex,birthday,phone, email, imgPath);
            System.out.println(user);

            Database database = new Database();
            database.connect();
            database.exec(user);
            Stage stage = (Stage) signInPane.getScene().getWindow();
            stage.close();

            String tip = "注册成功" + "\n账号: " + account + "\n密码: " + re_password.getText();
            new PBoxView(tip).show();

        }
    }

    /**
     * 检测输入数据是否合法并给出提示
     * 昵称、密码、重输密码、email不能为空
     * 检测DatePicker是否选择，未选择时将birthday置为“0”表示保密
     * 检测电话号码是否填写，未填写时将phone置为“0”表示保密
     * sex = "3"时性别保密
     * @return
     */
    private boolean checkInfo() {
        boolean flag = true;
        if (re_nickname.getText().isEmpty()) {
            label1.setText("昵称不能为空哦！");
            flag = false;
        } else {
            nickname = re_nickname.getText();
            label1.setText("");
        }
        if (re_password.getText().isEmpty()) {
            label2.setText("密码不合格哦！");
            flag = false;
        } else {
            password = re_password.getText();
            label2.setText("");
        }
        if (!re_recheck_pwd.getText().equals(re_password.getText())) {
            label3.setText("两次密码不一致！");
            flag = false;
        } else {
            label3.setText("");
        }
        if (re_birthday.getValue() != null) {

            birthday = re_birthday.getValue().toString();
        } else {
            birthday = "0";
        }
        if (re_phone.getText().isEmpty()) {
            phone = "0";
        } else {
            phone = re_phone.getText();
        }
        if (re_email.getText().isEmpty()) {
            label7.setText("邮箱必须填！");
            flag = false;
        } else {
            email = re_email.getText();
            label7.setText("");
        }
        return flag;
    }

    /**
     * 随机生成账号
     * @return
     */
    private String createAccountNum() {
        Random random = new Random(System.currentTimeMillis());
        int num = (int) ((random.nextDouble()*9 + 1) * 1000000);
        return String.valueOf(num);
    }

    /**
     * 选择头像方法
     * @param actionEvent
     * @throws IOException
     */
    public void choosePortrait(ActionEvent actionEvent) throws IOException {
        RegisterView view = (RegisterView) signInPane.getScene().getWindow();
        new PortraitView(view).show();
    }
}
